<template>
	<view class="wx-createGroup" :style="'height: calc(100vh - ' + windowTop + 'px)'">
		<watermark></watermark>
		<view class="top_text">
			<text class="lr">最近聊天</text>
			<text class="rr" @click="goTxl">从通讯录选择</text>
			<view style="clear: both;"></view>
		</view>
		<view class="wx-createGroup-check"><uni-indexed-list-wx showSelect :showAdd="false" :options="list" @click="bindClick"></uni-indexed-list-wx></view>
		<view style="height: 60px;"></view>
		<view class="wx-createGroup-foot">
			<view class="wx-btn" @click="sendMsg">
				完成
				<text v-if="select.length > 0">({{ select.length }})</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			list: [],
			select: [],
			windowTop: 0,
			data: ''
		};
	},
	onLoad(e) {
		this.data = JSON.parse(e.data);
	},
	mounted() {
		this.chatlist();
		uni.getSystemInfo({
			success: res => {
				this.windowTop = res.windowTop;
			}
		});
	},
	methods: {
		chatlist() {
			var chatlist = this.$store.state.chatlist;
			if(chatlist){
				var list = [];
				for(var i in chatlist){
					if(chatlist[i].nickName){
						var newObj = {};
						newObj.name = chatlist[i].nickName;
						newObj.userId = chatlist[i].userType + '_' + chatlist[i].userId;
						newObj.avatar = chatlist[i].userType == 'GROUP' ? JSON.parse(chatlist[i].portrait) : chatlist[i].portrait;
						newObj.avatar_type = chatlist[i].userType == 'GROUP' ? 'obj' : 'string';
						newObj.time = new Date(chatlist[i].time).getTime().toString();
						list.push(newObj);
					}
				}
				list.sort((a, b) => {
					return b['time'] - a['time'];
				});
				var newList = [{
					letter: '',
					data: list
				}];
				this.list = newList;
			}
		},
		sendMsg() {
			if(this.select.length <= 0){
				uni.showToast({
					title: '未选择',
					icon: 'none'
				})
				return
			}
			for (var i = 0; i < this.select.length; i++) {
				var userId = this.select[i].data.userId;
				var arrid = userId.split('_');
				var userId = arrid[1];
				if(arrid[0] == 'GROUP'){
					var windowType = 'GROUP';
				}else{
					var windowType = 'SINGLE';
				}
				this.$fc.pushOutMsg({
					msgContent: this.data.content,
					msgType: this.data.msgType,
					windowType: windowType,
					userId: userId
				});
			}
			uni.$emit('manyMsgSendSuccess',{msg:'多条消息转发成功'});
			this.$fc.ToastBack('已转发');
		},
		bindClick(e) {
			this.select = e.select;
		},
		goTxl(){
			uni.navigateTo({
				url: this.$fc.get_package_name()+'message_zhuanfa/tongxunlu?data=' + JSON.stringify(this.data)
			})
		}
	}
};
</script>

<style scoped>
.top_text{
	padding: 30rpx 20rpx;
	font-size: 28rpx;
}
.top_text text.lr{
	font-weight: 600;
}
.top_text text.rr{
	color: steelblue;
	float: right;
	font-weight: 600;
}
.wx-createGroup {
	display: flex;
	flex-direction: column;
}
.wx-createGroup-check {
	height: 90%;
	position: relative;
}
.wx-createGroup-foot {
	width: 100%;
	height: 60px;
	line-height: 60px;
	background-color: #eee;
	position: fixed;
	bottom: 0;
	left: 0;
	text-align: right;
}
.wx-btn {
	height: 40px;
	line-height: 40px;
	min-width: 200rpx;
	text-align: center;
	border-radius: 12rpx;
	background-color: #05c160;
	color: #fff;
	font-size: 32rpx;
	display: inline-block;
	margin-top: 10px;
	margin-right: 20rpx;
}
</style>
